<?php

namespace app\admin\model\fastim;

use think\Model;
use traits\model\SoftDelete;

class UserGroup extends Model
{

    use SoftDelete;


    // 表名
    protected $name = 'fastim_user_group';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'user_number'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getApplyList()
    {
        return ['0' => __('Apply 0'), '1' => __('Apply 1')];
    }

    public function getAgreeList()
    {
        return ['0' => __('Agree 0'), '1' => __('Agree 1')];
    }

    public function getGroupCheckList()
    {
        return ['0' => __('关闭'), '1' => __('开启')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getUserNumberAttr($value, $data)
    {
        return \think\Db::name('fastim_user')->where('type', 'user')->where('group_id', $data['id'])->count('id');
    }

}
